﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using  Models;
using BLL;
namespace Web
{
    public partial class admin_service : System.Web.UI.Page
    {
        private const int PAGESIZE = 10;
        private int PAGECOUNT;
        protected void Page_Load(object sender, EventArgs e)
        {
            int cid = 1;
            int page = 1;
            cid = InitFromUrl("cid", 1);
            page = InitFromUrl("page", 1);
            PAGECOUNT = ServiceInfoLogic.GetPageCount(PAGESIZE,cid);
            if (!IsPostBack)
            {
                BindData(cid, page);
            }
        }
        /// <summary>
        /// 从url中获取参数
        /// </summary>
        /// <param name="url"></param>
        /// <param name="first"></param>
        /// <returns></returns>
        private int InitFromUrl(string url,int first)
        {
            int rtn = first;
            try
            {
                rtn = Int32.Parse(Request.QueryString[url].ToString());
            }
            catch { }
            return rtn;
        }
        private void BindData(int cid,int page){
            BindServiceInfoList(cid, page);
            BindPagerList(cid);
            BindPagerLink(page,cid);
            BindNotice();
        }
        private void BindPagerList(int cid)
        {
            List<string> page = new List<string>();
            for (int i = 1; i <= PAGECOUNT; i++)
            {
                page.Add(i+"&cid="+cid.ToString());
            }
            NewsListPage.DataSource = page;
            NewsListPage.DataBind();
        }
        private void BindPagerLink(int nowPage,int cid)
        {
            int nextPage = nowPage + 1 > PAGECOUNT ? PAGECOUNT : nowPage + 1;
            int prePage = nowPage - 1 > 0 ? nowPage - 1 : 1;

            hlFirst.NavigateUrl = "?page=1&cid=" + cid.ToString();
            hlLast.NavigateUrl = "?page=" + PAGECOUNT.ToString()+"&cid="+cid.ToString();
            hlNext.NavigateUrl = "?page=" + nextPage.ToString() + "&cid=" + cid.ToString();
            hlPre.NavigateUrl = "?page=" + prePage.ToString() + "&cid=" + cid.ToString();
        }
        private void BindServiceInfoList(int cid,int page) {
            List<ServiceInfo> serviceList = ServiceInfoLogic.GetCategoryServiceInfo(cid, PAGESIZE, page);
            int i = 0;
            foreach (var item in serviceList)
            {
                item.CategoryName = ServiceCategoryLogic.GetOneServiceCategory(item.CategoryID).CategoryName;
                if (item.ServiceURL.IndexOf("\"") > -1)
                {
                    item.ServiceURL = item.ServiceURL.Substring(item.ServiceURL.IndexOf("\"") + 1, item.ServiceURL.LastIndexOf("\"") - item.ServiceURL.IndexOf("\"") - 1);
                }
                i++;
                item.ShowID = i + (page - 1) * PAGESIZE;
            }
            ServiceListRpt.DataSource = serviceList;
            ServiceListRpt.DataBind();
        }
        private void BindNotice()
        {
            noticefailed.Visible = false;
            noticesuccess.Visible = false;
        }

        protected void btnChange_Click(object sender, EventArgs e)
        {
            if (Request.Form["ckb"] == null)
            {
                return;
            }
            string[] tmp = Request.Form["ckb"].Split(',');
            int[] sids = new int[tmp.Count()];
            for (int i = 0; i < tmp.Count(); i++)
            {
                sids[i] = Int32.Parse(tmp[i]);
            }

            int isSuccess = 1;
            foreach (var sid in sids)
            {
                if (ServiceInfoLogic.DeleteServiceInfo(sid) != 1)
                {
                    isSuccess = -1;
                }
            }

            if (isSuccess != 1)
            {
                ChangeStatus(false);
            }
            else
            {
                ChangeStatus(true);
            }

            int cid = InitFromUrl("cid", 1);
            int page = InitFromUrl("page", 1);
            BindServiceInfoList(cid,page);
        }

        private void ChangeStatus(bool flag)
        {
            noticesuccess.Visible = flag ? true : false;
            noticefailed.Visible = flag ? false : true;
            ScriptManager.RegisterStartupScript(this, this.GetType(), "initclosenotice", "AjaxChange();", true);
        }

        protected void ibtnDelete_Click(object sender, EventArgs e)
        {
            ImageButton ib = (ImageButton)sender;
            int ServiceID = Int32.Parse(ib.ToolTip.ToString());
            if (ServiceInfoLogic.DeleteServiceInfo(ServiceID) != 1)
            {
                ChangeStatus(false);
            }
            else {
                ChangeStatus(true);
            }

            int cid = InitFromUrl("cid", 1);
            int page = InitFromUrl("page", 1);
            BindServiceInfoList(cid, page);
        }

    }
}
